package com.itcast.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.reggie.Dto.DishDto;
import com.itcast.reggie.pojo.*;
import com.itcast.reggie.service.DishFlavorService;
import com.itcast.reggie.service.DishService;
import com.itcast.reggie.service.FoodClassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController{
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private FoodClassService foodClassService;

    //条件分页查询
    @GetMapping("/page")
    public R<Page> GetByPage(int page, int pageSize, String name) {
        Page<Dish> page1 = new Page(page, pageSize);
        //创建一个空的page对象 可以使用setRecords导入数据
        //Page<DishDto> dishDtoPage = new Page<>();
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), "name", name);
        wrapper.orderByDesc("create_time");
        //执行了分页查询

         dishService.page(page1, wrapper);

        //对象拷贝 不导入数据

        //BeanUtils.copyProperties(page1,dishDtoPage,"records");
        List<Dish> records = page1.getRecords();

        List<Dish> list = records.stream().map((record) -> {
            DishDto dishDto = new DishDto();
            //分类id
            BeanUtils.copyProperties(record, dishDto);
            Long categoryId = record.getCategoryId();
            //查出来的对象
            Category category = foodClassService.getById(categoryId);
            String ClassName = category.getName();
            dishDto.setCategoryName(ClassName);
            return dishDto;
        }).collect(Collectors.toList());
        //导入数据
        page1.setRecords(list);
        return R.success(page1);
    }

    //添加菜品
    @PostMapping
    public R<String> AddDish(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }

    //数据回写
    @GetMapping("/{id}")
    public R<DishDto> Rewrite(@PathVariable Long id){

        //根据id 从DishServiceImpl 和 DishFlavorServiceImpl 里面查数据,再装进DishDto 里面
        //再返回去
        DishDto dishDto = new DishDto();
        //导入Dish 数据
        Dish dishData = dishService.getById(id);
        BeanUtils.copyProperties(dishData,dishDto);
        //导入DishFlavor 数据
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper();
        wrapper.eq("dish_id",id);
        List<DishFlavor> list = dishFlavorService.list(wrapper);
        dishDto.setFlavors(list);
        //数据封装完毕,返回去
        return R.success(dishDto);
    }

    //菜品数据修改
    @PutMapping
    public R<String> updateDish(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    //菜品起售,停售状态改变
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status,Long[] ids){
        dishService.updateStatus(status, ids);
        return R.success("修改成功");
    }

    //菜品批量删除
    @DeleteMapping()
    public R<String> deleteByIds(Long[] ids){
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }

    //菜品列表,附带口味选择
    @GetMapping("/list")
    public R<List<Dish>> GetList(Long categoryId){
        //得到Dish的列表
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);
        wrapper.eq("status",1);
        List<Dish> dishlist = dishService.list(wrapper);
        //得到口味信息.封装进去
        List<Dish> dishDtos = dishlist.stream().map((dish) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            //查询出这个菜的对应口味选择列表
            QueryWrapper<DishFlavor> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("dish_id", dish.getId());
            List<DishFlavor> list1 = dishFlavorService.list(wrapper2);
            //封装到dishDto对象中
            dishDto.setFlavors(list1);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dishDtos);
    }


}


